package com.dalongxin.work.page.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.dalongxin.work.Constants;
import com.dalongxin.work.R;
import com.dalongxin.work.data.User;
import com.dalongxin.work.utils.SharedPreferencesUtils;
import com.dalongxin.work.view.LoadingDialog;
import com.google.gson.Gson;

public class BaseFragment extends Fragment {

    public final Gson gson = new Gson();
    protected SharedPreferencesUtils spUtils;
    protected User defaultAccount;
    private LoadingDialog loadingDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        spUtils = new SharedPreferencesUtils(getActivity(), "DLX");
    }

    public User getDefaultAccount() {
        defaultAccount = spUtils.getObject(Constants.Key.USER, User.class);
        return defaultAccount;
    }

    public void setDefaultAccount(User account) {
        if(account != null) {
            spUtils.setObject(Constants.Key.USER, account);
        }
        this.defaultAccount = account;
    }

    protected void showLoading() {
        if(loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity(), R.style.LoadingDialog);
        }
        loadingDialog.show();
    }

    protected void hideLoading() {
        if(loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}
